package com.ruoyi.system.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.common.rocketmqmsg.SendAiMsg;
import com.ruoyi.system.rollback.SendCallbackImpl;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 主界面接口控制层
 * @author zhaoYuFei
 */
@RestController
@RequestMapping("/main")
public class MainController extends BaseController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 生产者
     * 用户提问gpt
     * @param msg 提问信息
     * @return null
     */
    @GetMapping("/ai")
    public AjaxResult sendUserAIMsg(@RequestParam("msg") String msg){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //发送异步请求

        //构建message对象
        SendAiMsg sendAiMsg=new SendAiMsg(loginUser.getUserid(),msg);
        Message<SendAiMsg> message= MessageBuilder.withPayload(sendAiMsg).build();
        rocketMQTemplate.asyncSend("ai",message,new SendCallbackImpl(loginUser.getUsername()));
        return success();
    }
}
